﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Data;
using System.IO;
using System.Xml;

namespace VoIPWeb
{
    public partial class RecentCalls : System.Web.UI.Page
    {
        public override void VerifyRenderingInServerForm(Control control)
        {
            //base.VerifyRenderingInServerForm(control);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        #region Members
        public string m_str_email, m_str_account;
        public decimal m_dc_balance;
        string m_str_user = "aznovavoip";
        DataTable m_dt_data_table = new DataTable();
        #endregion
        #region Private Methods
        /// <summary>
        /// Hàm này lấy toàn bộ thông tin của customer và đổ lên lưới
        /// </summary>
        private void get_all_infor()
        {
            var v_str_user = Session["User"];
            var v_str_customer = Session["Customer"];
            var v_str_cpasswd = Session["Cpassword"];

            var v_str_api = string.Format("https://www.voipinfocenter.com/api/request.ashx?command=GetUserInfo&username={0}&password={1}&customer={2}*{0}", v_str_user, v_str_cpasswd, v_str_customer);

            HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create(v_str_api);
            HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
            XmlDataDocument dataXML = new XmlDataDocument();

            dataXML.Load(HttpWResp.GetResponseStream());
            XmlNodeList v_xml_customer_nodelist = dataXML.SelectNodes("/GetUserInfo/Customer");
            XmlNodeList v_xml_balance_nodelist = dataXML.SelectNodes("/GetUserInfo/Balance");
            XmlNodeList v_xml_email_nodelist = dataXML.SelectNodes("/GetUserInfo/EmailAddress");
            m_lbl_customer.Text = v_xml_customer_nodelist[0].InnerText;
            m_lbl_email.Text = v_xml_email_nodelist[0].InnerText;
            m_lbl_balance.Text = string.Format(v_xml_balance_nodelist[0].InnerText);
        }

        private string get_url(string ip_str_user, string ip_str_customer)
        {
            string v_str_password = Session["Cpassword"].ToString();
            return string.Format("https://www.voipinfocenter.com/api/request.ashx?command=GetUserInfo&username={0}&password={1}&customer={2}*{0}", ip_str_user, v_str_password, ip_str_customer);
        }

        private DataTable get_today_call_info()
        {
            DataTable v_dt_today_call = new DataTable();
            XmlDocument v_xml_doc_today = new XmlDocument();
            HttpWebRequest v_httpwebreq = (HttpWebRequest)WebRequest.Create(get_url(m_str_user, Session["Customer"].ToString()));
            HttpWebResponse v_httpwebrespon = (HttpWebResponse) v_httpwebreq.GetResponse();
            v_xml_doc_today.Load(v_httpwebrespon.GetResponseStream());
            string v_str_xpath = string.Format("//Call");
            // Lấy tất cả các node
            XmlNodeList v_xml_nodelist = v_xml_doc_today.SelectNodes(v_str_xpath);
            foreach (XmlNode v_xml_node in v_xml_nodelist)
            {
                v_xml_node.SelectSingleNode(".[@]");
            }
           
            // Lấy ra giá trị và so sánh
            v_dt_today_call.Columns.Add("Calls",typeof(int));            
            DataRow v_dr = v_dt_today_call.NewRow();
            v_dr["Calls"] = v_xml_nodelist.Count;
            v_dt_today_call.Rows.Add(v_dr);
            return v_dt_today_call;
        }
        #endregion
    }
}